#include "xservice_proxy_client.h"
#include <protobuf/xmsg_com.pb.h>
#include <XCom.h>
#include <XMsg.h>
#include <XTools.h>

// 构造函数XServiceProxyClient初始化对象
XServiceProxyClient::XServiceProxyClient()
{
}

void XServiceProxyClient::RegEvent(XMsgEvent *ev)
{
    std::unique_lock <std::mutex> lock(callback_task_mutex_);
    callback_task_[(long long)ev]=ev;
}

void XServiceProxyClient::DelEvent(XMsgEvent *ev)
{
    std::unique_lock <std::mutex> lock(callback_task_mutex_);
    callback_task_.erase((long long)ev);
}

void XServiceProxyClient::readCB(xmsg::XMsgHead *head, XMsg *msg)
{
    XCOM_LOG_ERROR("XServiceProxyClient::readCB start!");
    if(!head||!msg)
        return;
    std::unique_lock <std::mutex> lock(callback_task_mutex_);
    auto ruoter = callback_task_.find(head->msg_id());
    if(ruoter!=callback_task_.end())
    {
        ruoter->second->SendMsg(head,msg);
        return;
    }
    XCOM_LOG_ERROR("XServiceProxyClient::readCB not found callback task!");
}

bool XServiceProxyClient::SendMsg(xmsg::XMsgHead *head, XMsg *message, XMsgEvent *ev)
{
    RegEvent(ev);
    head->set_msg_id((long long)ev);
    return XServerClient::SendMsg(head,message);
}
